package cn.niit.letuseatdemo.controller.store;

import cn.niit.letuseatdemo.entity.domain.OrderGoods;
import cn.niit.letuseatdemo.service.OrderGoodsService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 订单控制器方法
 * @Author: su_fuming
 * @CreateTime： 2022/11/14 22:14
 */
@Slf4j
@RequestMapping("/store/order")
@Controller
public class OrderGoodsController {
        @Autowired
        private OrderGoodsService orderGoodsService;
        /**
         * @Description: 订单信息管理（列表）页面的初始化及查询
         * @Author: su_fuming
         * @CreateTime: 2022/11/8 20:39
         * @param: model
         * @param: ordergoods
         * @return: java.lang.String
         **/

        @GetMapping
        public String index(Model model, OrderGoods orderGoods) {
            model.addAttribute("orderGoods", orderGoods);
            List<OrderGoods> orderGoodsList = orderGoodsService.selectOrderGoodsList(orderGoods);
            PageInfo<OrderGoods> pageInfo = new PageInfo<>(orderGoodsList);
            log.info("分页对象 = {}", pageInfo);

            log.info("查询出来的订单信息列表：{}", orderGoodsList);
            //第一个参数时该数据保存在域对象中的key,在页面中渲染数据时通过orderGoodsList 来获取订单集合
            model.addAttribute("orderGoodsList", orderGoodsList);
            model.addAttribute("pageInfo", pageInfo);
            return "store/order/list";

        }
        /**
         * @Description: 根据ID删除订单信息
         * @Author: su_fuming
         * @CreateTime: 2022/11/8 20:47
         * @param: ordergoodsId
         * @return: java.lang.String
         **/

        @ResponseBody
        @DeleteMapping("/{orderId}")
        public String delete(@PathVariable("orderId") Long orderId){
            int rows =orderGoodsService.deleteOrderGoodsByOrderId(orderId);
            if (rows == 1){
                return "true";
            }else{
                return "false";
            }
        }
    /**
     * @Description: 菜品状态控制
     * @Author: su_fuming
     * @CreateTime: 2022/10/13 9:20 Unclosed character class near index 10
     * [\ [|\]|\s]
     * ^
     * @return: java.lang.String
     **/
    @GetMapping("/onDown/{orderId}")
    public String onDown(@PathVariable("orderId") Long orderId) {
        log.info("要修改的ID = " + orderId);
        OrderGoods orderGoods = null;
        if (null != orderId && orderId > 0) { //值的修改
            orderGoods = orderGoodsService.selectOrderGoodsByOrderId(orderId);
        }
        orderGoods.setOrderStatus((orderGoods.getOrderStatus() + 1)%6);
        orderGoodsService.updateOrderStatus(orderGoods);
        return "redirect:/store/order";
    }
}

